﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fis.Win.Forms
{
    public partial class UserListForm : ListForm
    {
        public UserListForm()
        {
            InitializeComponent();
        }

        public override void InitializeBinding()
        {
            gridControl1.DataSource = ViewModel["viewData"];
            Fis.Win.Utils.UIUtils.LookUpBinding(repState, Fis.Win.Utils.CacheContext.StateCache(), "display", "value");
            Fis.Win.Utils.UIUtils.LookUpBinding(repRights, Fis.Win.Utils.CacheContext.RightsCache(), "display", "value");
        }

        public override void InitializeViewUI()
        {
            iDel.Caption = "冻结";
            iXtra1.Caption = "权限设置";
            iXtra1.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
            iXtra1.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(iXtra1_ItemClick);
        }

        void iXtra1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Message = string.Empty;
            //var edit = (sender as DevExpress.XtraEditors.BaseEdit).EditValue;
            var data = gridView1.GetFocusedDataRow();

            if (data["FRIGHTS"].Equals("1"))
            {
                Message = "无权限，请联系客服";
                return;
            }

            var state = (Agile.Engine.Utils.StringUtils.IsEmpty(data["FRIGHTS"]) ? Agile.Engine.View.ViewState.AddNew : Agile.Engine.View.ViewState.Modify);
            var param = new System.Collections.Hashtable();
            param["userid"] = data["SID"];
            UIManager.ShowEdit<RightsInfoForm>("RightsInfoViewProvider", state, param);
        }

        protected override void NewProcess()
        {
            UIManager.ShowEdit<UserEditForm>("UserEditViewProvider");
            Fis.Win.Utils.CacheContext.RefreshCache(ViewProvider.DataTableName);

            RefreshProcess();
        }

        protected override void EditProcess()
        {
            if (gridView1.FocusedRowHandle < 0)
                return;


            Message = string.Empty;
            //var edit = (sender as DevExpress.XtraEditors.BaseEdit).EditValue;
            var data = gridView1.GetFocusedDataRow();

            if (data["FRIGHTS"].Equals("1"))
            {
                Message = "无权限，请联系客服";
                return;
            }


            //var data = gridView1.GetDataRow(gridView1.FocusedRowHandle);

            UIManager.ShowEdit<UserEditForm>("UserEditViewProvider"
                            , Agile.Engine.View.ViewState.Modify
                            , Agile.Engine.Utils.DataUtils.ToEditModel("viewData", data));
            Fis.Win.Utils.CacheContext.RefreshCache(ViewProvider.DataTableName);

            RefreshProcess();
        }

        protected override void DelProcess()
        {
            if (gridView1.FocusedRowHandle < 0)
                return;


            Message = string.Empty;
            //var edit = (sender as DevExpress.XtraEditors.BaseEdit).EditValue;
            var data = gridView1.GetFocusedDataRow();

            if (data["FRIGHTS"].Equals("1"))
            {
                Message = "无权限，请联系客服";
                return;
            }

            //var data = gridView1.GetDataRow(gridView1.FocusedRowHandle);

            if (DialogResult.Yes == DevExpress.XtraEditors.XtraMessageBox.Show("确定要冻结吗？", "提示", MessageBoxButtons.YesNo))
            {
                ViewModel["SID"] = data["SID"];
                ViewModel["Action"] = "Delete";
                ViewProvider.ExecuteUpdate(ViewModel);
                Fis.Win.Utils.CacheContext.RefreshCache(ViewProvider.DataTableName);

                RefreshProcess();
            }
        }


        private void UserListForm_Load(object sender, EventArgs e)
        {

        }

        private void repRights_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Message = string.Empty;
            var edit = (sender as DevExpress.XtraEditors.BaseEdit).EditValue;
            var data = gridView1.GetFocusedDataRow();

            if (data["FRIGHTS"].Equals("1"))
            {
                Message = "不能对主管设置权限";
                return;
            }

            var state = ((edit == null) ? Agile.Engine.View.ViewState.AddNew : Agile.Engine.View.ViewState.Modify);
            var param = new System.Collections.Hashtable();
            param["userid"] = data["SID"];
            UIManager.ShowEdit<RightsInfoForm>("RightsInfoViewProvider", state, param);
        }

        public override void ToXls(string path)
        {
            var option = new DevExpress.XtraPrinting.XlsExportOptions();
            option.SheetName = Text;
            gridControl1.ExportToXls(path, option);
        }
    }
}
